package serivce;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.kernel.Config;
import com.alipay.easysdk.kernel.util.ResponseChecker;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import org.springframework.stereotype.Service;

import java.util.ResourceBundle;
import java.util.UUID;

@Service
public class AliPayImp implements AliPay{

    private static Config setOPtion(){
        ResourceBundle res= ResourceBundle.getBundle("conf/pay");
        Config f=new Config();
        f.alipayPublicKey= res.getString("alipayPublicKey");
        f.appId=res.getString("appid");
        f.merchantPrivateKey=res.getString("privateKey");
        f.protocol = "https";
        f.gatewayHost = "openapi.alipaydev.com";
        f.signType = "RSA2";
        f.notifyUrl="http://3552b08h95.zicp.vip:29131/houseDetail/paydata";
        return f;
    }

    @Override
    public String computerPay(String price) {
        com.alipay.easysdk.factory.Factory.setOptions(setOPtion());
        AlipayTradePagePayResponse response=null;
        try {
            String id= UUID.randomUUID().toString();
            String returnurl="http://localhost:8080/#/personal/historyOrder";
            response= Factory.Payment.Page().pay("租房",id,price,returnurl);
            // 3. 处理响应或异常
            if (ResponseChecker.success(response)) {
                System.out.println(response);
                System.out.println("调用成功");
            } else {
                System.err.println("调用失败，原因：" + response.toString());
            }
        } catch (Exception e) {
            System.err.println("调用遭遇异常，原因：" + e.getMessage());
            throw new RuntimeException(e.getMessage(), e);
        }
        return  response.body;
    }
}
